<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link href="/css/common.css" rel="stylesheet" type="text/css" />
<style>
.wrapContent {margin:0 auto;padding:0 30px;}
.center {text-align:center;}
.block {padding:10px 30px;}
.line {border:none;border-bottom:1px solid #666;clear:both;}
.orderSN {float:left;}
.orderMan {float:right;margin:-6px 20px 0 0;}
ul.machineType{width:80%;}
ul.machineType li{float:left;width:33%;line-height:26px;height:26px;}
.lineHeight{height:34px;line-height:34px;}
.right {text-align:right;width:120px;vertical-align:top}
.errMsg {color:red;margin:0 0 0 5px;}
.green {color:green;}
.red {color:red;}
</style>
</head>
<body>
<div class="wrapContent">
	<div class="block">
		<h2 class="center">新建订单</h2>
		<span class="orderSN">订单号：<span id="orderSN"><?php echo $SN; ?></span></span>
		<span class="orderMan">下单人：<input id="orderMan" /><b id="orderManErrMsg" class="errMsg">*</b></span>
	</div>
	<div class="line"></div>
	<div class="block">
		<table style="width:100%;border:none;" cellpadding="0" cellspacing="0">
			<tr class="lineHeight">
				<td class="right">客户信息：</td>
				<td>
					<select id="customerInfoSelect">
					<?php
						echo "<option value='-1'>请选择</option>";
						foreach($customerList['data'] as $customer){
							echo "<option value='{$customer['id']}'>{$customer['name']}</option>";
						}
					?>
					</select>
					<b id="customerInfoErrMsg" class="errMsg"></b>
				</td>
			</tr>
			<tr class="lineHeight">
				<td class="right">办公点：</td>
				<td>
					<select id="childNameSelect">
						<option value='-1'>请选择</option>
					</select>
					<b id="childNameErrMsg" class="errMsg"></b>
				</td>
			</tr>
			<tr class="lineHeight">
				<td class="right">客户信息：</td>
				<td><input id="customerInfoInput" style="width:500px;"/><b id="customerInfoInputErrMsg" class="errMsg">*</b></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">地区：</td>
				<td><select id="province" name="province"></select> <select id="city" name="city"></select> <select id="area" name="area"></select><b id="areaErrMsg" class="errMsg">*</b></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">详细地址：</td>
				<td><input id="address" style="width:700px;"/><b id="addressErrMsg" class="errMsg">*</b></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">邮政编码：</td>
				<td><input id="zipcode" style="width:200px;"/></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">联系人：</td>
				<td><input id="contact" style="width:200px;"/><b id="contactErrMsg" class="errMsg">*</b></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">电话：</td>
				<td><input id="tel" style="width:200px;"/><b id="telErrMsg" class="errMsg">*</b></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">Email：</td>
				<td><input id="email" style="width:200px;"/><b id="emailErrMsg" class="errMsg"></b></td>
			</tr>
		</table>
	</div>
	<div class="line"></div>
	<div class="block">
		<table style="width:100%;border:none;" cellpadding="0" cellspacing="0">
			<tr class="lineHeight">
				<td class="right">机器名称：</td>
				<td><select id="machineName">
				<option value="">请选择</option>
				<?php
				if (!empty($machines['data'])) {
					foreach($machines['data'] as $machine) {
						echo "<option value='{$machine['id']}' id='{$machine['id']}'>{$machine['name']}</option>";
					}
				}
				?>
				</select><b id="machineNameErrMsg" class="errMsg">*</b></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">机器型号：</td>
				<td><input id="machineType" style="width:200px;"/><b id="machineTypeErrMsg" class="errMsg"></b></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">机器序号：</td>
				<td><input id="machineSN" style="width:200px;"/><b id="machineSNErrMsg" class="errMsg">*</b></td>
			</tr>
			<tr class="lineHeight">
				<td class="right">耗材型号：</td>
				<td>
					<table id="cartridgesInfo" cellpadding="0" cellspacing="0">
						<!-- 
						<tr>
							<td width="20%"><input id="cartridgesType_0" type="checkbox" name="cartridgesType" /><label>型号1</label></td>
							<td width="20%">个数：<input id="cartridgesType_0_count" style="width:80px;"/></td>
							<td width="10%">北京：20</td>
							<td width="10%">上海：20</td>
							<td width="10%">成都：20</td>
							<td width="10%">广州：20</td>
						</tr>
						 -->
					</table>
					<b id="cartridgesInfoErrMsg" class="errMsg">*</b>
				</td>
			</tr>
		</table>
	</div>
	<div class="line"></div>
	<div class="center">
		<input id="submit" type="button" class="submitBtn" value="提交订单"/>
	</div>
</div>
</body>
<script src="/js/lib.min.js" type="text/javascript"></script>
<script src="/js/pcas.min.js" type="text/javascript"></script>
<script type="text/javascript">
<?php
echo "var warehouse = " . json_encode($warehouse) .";\n";
?>
function getSelectValue(id) {
	var obj = $('#'+id)[0];
	return obj.options[obj.selectedIndex].value;
}
function getSelectText(id) {
	var obj = $('#'+id)[0];
	return obj.options[obj.selectedIndex].text;
}
function getSelectIdValue(id) {
	var obj = $('#'+id)[0];
	return obj.options[obj.selectedIndex].attribute('id');
}
function getArea(id) {
	return getSelectValue(id);
}
new PCAS("province","city","area");
var orderManInput = $('#orderMan'),
	orderManErrMsg = $('#orderManErrMsg'),
	customerInfoInput = $('#customerInfoInput'),
	customerInfoInputErrMsg = $('#customerInfoInputErrMsg'),
	customerInfoSelect = $('#customerInfoSelect'),
	customerInfoErrMsg = $('#customerInfoErrMsg'),
	childNameSelect = $('#childNameSelect'),
	childNameErrMsg = $('#childNameErrMsg'),
	areaErrMsg = $('#areaErrMsg'),
	addressInput = $('#address'),
	addressErrMsg = $('#addressErrMsg'),
	zipcodeInput = $('#zipcode'),
	zipcodeErrMsg = $('#zipcodeErrMsg'),
	contactInput = $('#contact'),
	contactErrMsg = $('#contactErrMsg'),
	telInput = $('#tel'),
	telErrMsg = $('#telErrMsg'),
	emailInput = $('#email'),
	emailErrMsg = $('#emailErrMsg'),
	machineNameSelect = $('#machineName'),
	machineNameErrMsg = $('#machineNameErrMsg'),
	machineTypeInput = $('#machineType'),
	machineTypeErrMsg = $('#machineTypeErrMsg'),
	machineSNInput = $('#machineSN'),
	machineSNErrMsg = $('#machineSNErrMsg'),
	cartridgesInfo = $('#cartridgesInfo'),
	cartridgesInfoErrMsg = $('#cartridgesInfoErrMsg'),
	submitBtn = $('#submit');


function resetUserInfo() {
	$('#province')[0].options.length=0;
	new PCAS("province","city","area");
	addressInput.val('');
	addressErrMsg.val('*');
	zipcodeInput.val('');
	contactInput.val('');
	contactErrMsg.val('*');
	telInput.val('');
	telErrMsg.val('*');
	emailInput.val('');
	emailErrMsg.val('');
}

customerInfoSelect.attr({onchange:function(){
	var id = getSelectValue('customerInfoSelect');
	if (id < 0) {
		childNameSelect[0].options.length=1;
		resetUserInfo();
		return ;
	}
	
	$.ajax({
		url:'/admin/customer.php?op=getChild',
		data:{id:id},
		type:'POST',
		callback:function(res){
			if (res.errno == '0') {
				childNameSelect[0].options.length=1;
				for(i=0, cnt=res.data.length; i<cnt; i++){
					var child = res.data[i];
					childNameSelect[0].options.add(new Option(child.name, child.index));
				}
			}else {
				alert(res.errmsg);
			}
		}
	});
}});

childNameSelect.attr({onchange:function(){
	var childValue = getSelectValue('childNameSelect');
	if (childValue < 0) {
		resetUserInfo();
		return false;
	}
	
	$.ajax({
		url:'/admin/customer.php?op=getDetail',
		data:{idx:childValue},
		type:'POST',
		callback:function(res){
			if (res.errno == '0') {
				customerInfoInput.val(getSelectText('customerInfoSelect')+"("+getSelectText('childNameSelect')+")");
				var data = res.data;
				var areaArray = data.area.split('_');
				$('#province')[0].options.length=0;
				new PCAS('province', 'city', 'area', areaArray[0], areaArray[1], areaArray[2]);
				areaErrMsg.html('*');
				addressInput.val(data.address);
				addressErrMsg.html('*');
				zipcodeInput.val(data.zipcode);
				contactInput.val(data.contact);
				contactErrMsg.html('*');
				telInput.val(data.tel);
				telErrMsg.html('*');
				emailInput.val(data.email);
				emailErrMsg.html('');
				detailID = data.id;
			}else {
				alert(res.errmsg);
			}
		}
	});	
}});

machineNameSelect.attr({onchange:function(){
	var value = getSelectValue('machineName');
	if (!value) {
		return false;
	}
	
	$.ajax({
		url:'/admin/machine.php?op=getCartridgesInfo',
		data:{mid:value},
		type:'POST',
		callback:function(res){
			if (res.errno == '0') {
				machineTypeInput.val('');
				var innerHTML = '';
				$.each(res.data, function(item, idx){
					if ($.isArray(item))return;
					var inventoryHTML = '';
					$.each(item.inventory, function(v, k){
						inventoryHTML += '<td width="10%">'+warehouse[k]+':'+(v>0?'<span class="green">有货</span>':'<span class="red">无货</span>')+'</td>';
					});
					var html = '<td width="20%">'+idx+'</td><td width="20%">个数：<input id="cartridgesType_'+idx+'_count" mtype="'+idx+'"style="width:80px;" value="0"/></td>';
					innerHTML += '<tr>'+html+inventoryHTML+'</tr>';
				});
				cartridgesInfo.html(innerHTML);
			}else {
				alert(res.errmsg);
			}
		}
	});
}});

submitBtn.attr({onclick:function(){
	var checkFlag = true;
	var orderMan = $.trim(orderManInput.val());
	if (!orderMan){
		orderManErrMsg.html('请输入');
		checkFlag && orderManInput[0].focus();
		checkFlag = false;
	}else {
		orderManErrMsg.html('*');
	}
	
	var customerInfoInputValue = $.trim(customerInfoInput.val());
	if (!customerInfoInputValue) {
		customerInfoInputErrMsg.html('请填写客户信息');
		checkFlag && customerInfoInput[0].focus();
		checkFlag = false;
	}else {
		customerInfoInputErrMsg.html('*');
	}
	/*
	var customerInfoValue = getSelectValue('customerInfoSelect');
	if (customerInfoValue < 0 && !customerInfoInputValue) {
		customerInfoErrMsg.html('请选择客户信息');
		checkFlag && customerInfoSelect[0].focus();
		checkFlag = false;
	}else {
		customerInfoErrMsg.html('*');
	}
	
	var customerChildInfoValue = getSelectValue('childNameSelect');
	if (customerChildInfoValue < 0 && !customerInfoInputValue) {
		childNameErrMsg.html('请选择办公点');
		checkFlag && childNameSelect[0].focus();
		checkFlag = false;
	}else {
		childNameErrMsg.html('*');
	}
	*/
	
	var province = getArea('province'),
		city = getArea('city'),
		area = getArea('area');
	city = (city.indexOf('市辖') >= 0) ? '' : city;
	area = (area.indexOf('市辖') >= 0) ? '' : area;
	if (!province) {
		areaErrMsg.html('请选择具体地区位置');
		checkFlag && $('#province')[0].focus();
		checkFlag = false;
	}else {
		areaErrMsg.html('*');
	}
		
	var address = $.trim(addressInput.val());
	if (!address) {
		addressErrMsg.html('请输入详细地址');
		checkFlag && addressInput[0].focus();
		checkFlag = false;
	}else {
		addressErrMsg.html('*');
	}
	
	var zipcode = $.trim(zipcodeInput.val());
	/*
	if (!zipcode || zipcode.length != 6 || parseInt(zipcode) != zipcode) {
		zipcodeErrMsg.html('请正确输入邮政编码');
		console.log(zipcodeErrMsg);
		checkFlag && zipcodeInput[0].focus();
		checkFlag = false;
	}else {
		zipcodeErrMsg.html('*');
	}
	*/
	
	var contact = $.trim(contactInput.val());
	if (!contact) {
		contactErrMsg.html('请输入联系人');
		checkFlag && contactInput[0].focus();
		checkFlag = false;
	}else {
		contactErrMsg.html('*');
	}
	
	var tel = $.trim(telInput.val());
	if (!tel || (!/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(tel) && !/\d{11}/.test(tel))) {
		telErrMsg.html('请正确输入电话号码');
		checkFlag && telInput[0].focus();
		checkFlag = false;
	}else {
		telErrMsg.html('*');
	}
	
	var email = $.trim(emailInput.val());
	if (email && email.indexOf('@') < 0) {
		emailErrMsg.html('无效的email地址');
		checkFlag && emailInput[0].focus();
		checkFlag = false;
	}else {
		emailErrMsg.html('');
	}
	
	var machineName = getSelectText('machineName');
	if (machineName && machineName=='请选择') {
		machineNameErrMsg.html('请选择机器名称');
		checkFlag && machineNameSelect[0].focus();
		checkFlag = false;
	}else {
		machineNameErrMsg.html('*');
	}
	
	var machineType = $.trim(machineTypeInput.val());
	/*
	if (!machineType) {
		machineTypeErrMsg.html('请输入机器类型');
		checkFlag && machineTypeInput[0].focus();
		checkFlag = false;
	}else{
		machineTypeErrMsg.html('*');
	}
	*/
	
	var machineSN = $.trim(machineSNInput.val());
	if (!machineSN) {
		machineSNErrMsg.html('请输入机器序列号');
		checkFlag && machineSN[0].focus();
		checkFlag = false;
	}else {
		machineSNErrMsg.html('*');
	}
	
	var cartridgeCountInfo = '';
	$('input', cartridgesInfo).each(function(input, idx){
		var count = $(input).val()|0;
		if (count != $(input).val()) {
			cartridgeCountInfo = '';
			checkFlag && cartridgesInfoErrMsg.html('硒鼓数量只能填写数字，请正确填写');
			checkFlag = false;
		}else if (count > 0) {
			cartridgeCountInfo += $(input).attr('mtype')+'_'+count+',';
		}
	});
	if (!cartridgeCountInfo) {
		checkFlag && cartridgesInfoErrMsg.html('请填写硒鼓数量');
		checkFlag  = false;
	}else {
		cartridgeCountInfo = cartridgeCountInfo.substring(0, cartridgeCountInfo.length-1);
	}
	
	if (!checkFlag) {
		return ;
	}
	
	var orderSN = $.trim($('#orderSN').html());
	//var customerName = getSelectValue('customerInfoSelect') < 0 ? customerInfoInputValue : getSelectText('customerInfoSelect')+"("+getSelectText('childNameSelect')+")";
	var postData = {
		'sn':orderSN,
		'orderMan':orderMan,
		'customerName':customerInfoInputValue,
		'area':province+'_'+city+'_'+area,
		'address':address,
		'zipcode':zipcode,
		'contact':contact,
		'tel':tel,
		'email':email,
		'machineName':machineName,
		'machineType':machineType,
		'machineSN':machineSN,
		'cartridgeCountInfo':cartridgeCountInfo
	}
	
//	console.log('data', postData);
	submitBtn.val('正在处理，请稍后...');
	submitBtn[0].disabled = true;
	
	$.ajax({
		url:'/admin/order.php?op=doAdd',
		data:postData,
		type:'POST',
		callback:function(res){
			alert(res.errmsg);
			submitBtn.val('提交订单')
			submitBtn[0].disabled = false;
			if (res.errno == '0'){
				location.reload();	
			}
		}
	});
	
}});
</script>
</html>